package cn.edu.xmist.supermarket.wtlvgcl.controller;

import cn.edu.xmist.supermarket.wtlvgcl.config.Vars;
import cn.edu.xmist.supermarket.wtlvgcl.entity.YonghuPO;
import cn.edu.xmist.supermarket.wtlvgcl.service.YonghuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/yonghu")
public class YonghuController {

  @Autowired private YonghuService yonghuService;
  @Autowired private HttpSession httpSession;

  @RequestMapping("/loginHtml")
  public String login() {
    return "yonghu";
  }

  @ResponseBody
  @RequestMapping("/login")
  public String login(YonghuPO user) {
    QueryWrapper<YonghuPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("username", user.getUsername());
    List<YonghuPO> list = yonghuService.list(queryWrapper);
    if (list.size() > 0) {
      if (user.getPassword().equals(list.get(0).getPassword())) {
        httpSession.setAttribute("user", user.getUsername());
        return Vars.SUCCESS;
      }
    }
    return Vars.FAILURE;
  }

  @ResponseBody
  @RequestMapping("/logout")
  public String logout() {
    httpSession.invalidate();
    return Vars.SUCCESS;
  }
}
